package com.sjzs.web.action;

import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sjzs.web.dao.Pic000Dao;
import com.sjzs.web.po.Pic000;

public class PictureAction extends ActionSupport {

	private static final long serialVersionUID = 2345305420620112421L;
	private static final Logger LOGGER = Logger.getLogger(PictureAction.class);

	private transient Pic000Dao pictureDao;
	private Long id;

	public void getPicture() {
		if (id != null) {
			Pic000 picture = pictureDao.findById(id);
			if (picture != null) {
				LOGGER.info("----------output sign image with id " + id);
				byte[] imageBytes = picture.getTpblob();
				doImageOutput(imageBytes);
			}
		}
	}

	private void doImageOutput(byte[] imageBytes) {
		HttpServletResponse response = ServletActionContext.getResponse();
		ServletOutputStream out = null;
		try {
			response.setContentType("multipart/form-data");
			out = response.getOutputStream();
			out.write(imageBytes);
		} catch (IOException e) {
			LOGGER.error("IOException when write image info to response");
		}
	}

	public void setPic000Dao(Pic000Dao pictureDao) {
		this.pictureDao = pictureDao;
	}

	public void setId(Long id) {
		this.id = id;
	}

}
